package com.ruoyi.autoTask;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.total.autoInsect.controller.AutoInsectController;
import com.ruoyi.xag.controller.IotController;
import com.ruoyi.ys7Video.controller.VideoController;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 设置定时任务
 *
 * @author Mr-Le
 */
@Component
@Configuration
@EnableScheduling
public class AutoTaskTest {

    @Scheduled(cron = "0 1 12 * * ?")
    private void topImg() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String end = format.format(date);

        System.out.println("开始执行获取虫情测报灯图片");
        AutoInsectController autoInsectController = SpringUtils.getBean(AutoInsectController.class);
        autoInsectController.downAndDetection(beforeHourToNowDate(end, 12), end);
//        autoInsectController.downAndDetection("20210331024000", "20210331024300");
        System.out.println("执行完成");
    }

    /**
     * 自定抓拍图片
     * 暂时关闭
     */
//    @Scheduled(cron = "0 0 0/1 * * ? ")
    private void captureImg() {
        System.out.println("开始执行获取虫情测报灯图片");
        VideoController videoController = SpringUtils.getBean(VideoController.class);
        // 球机摄像头
        AjaxResult result = videoController.CaptureImgBySerial("F94404261");
        System.out.println("情况：" + result);
    }


    /**
     * 物联网设备更新数据
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    private void iotDevice() {
        System.out.println("开始执行物联网设备信息更新");
        IotController iotController = SpringUtils.getBean(IotController.class);
        AjaxResult result = iotController.dataPointsLast();
        // 执行情况说明
        System.out.println("情况：" + result);
    }



    /**
     * 设置某一时间 此前某小时 的 date
     * 返回类型为 Date： return c.getTime();
     * String： return sdf.format(c.getTime());
     *
     * @param date  字符串类型的date
     * @param hours 小时
     * @return 本次用到String类型，修改为String
     */
    public static String beforeHourToNowDate(String date, int hours) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            c.setTime(sdf.parse(date));
            c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) - hours);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdf.format(c.getTime());
    }

}
